package com.itheima.web.manager;

import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.FreezeService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class FreezeManager {
    @DubboReference
    private UserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @DubboReference
    private FreezeService freezeService;
    @DubboReference
    private UserService userService;

    public String userFreeze(String userId, String freezingTime, String freezingRange, String reasonsForFreezing, String frozenRemarks) {
        //保存冻结信息

        UserFreeze userFreeze = new UserFreeze();
        userFreeze.setUserId(Long.valueOf(userId));
        userFreeze.setFreezingTime(Long.valueOf(freezingTime));
        userFreeze.setFreezingRange(Long.valueOf(freezingRange));
        userFreeze.setReasonsForFreezing(reasonsForFreezing);
        userFreeze.setFrozenRemarks(frozenRemarks);
        freezeService.save(userFreeze);

        //将冻结时间和冻结范围放入redis
        long time=0L;
        if (StrUtil.equals("1",freezingTime)) {
        time=3L;
        }
        if (StrUtil.equals("2",freezingTime)) {
            time=7L;
        }
        if (StrUtil.equals("3",freezingTime)) {
            time=36500L;
        }
        stringRedisTemplate.opsForValue().set(ConstantUtil.FREEZE_TIME+userId,freezingTime,time, TimeUnit.DAYS);
        stringRedisTemplate.opsForValue().set(ConstantUtil.FREEZE_RANGE+userId,freezingRange,time, TimeUnit.DAYS);



        //修改用户信息重点冻结装状态
        UserInfo userInfo = userInfoService.findByUserId(Long.valueOf(userId));
        userInfo.setUserStatus("2");
        try {
            userInfoService.update(userInfo);
        } catch (Exception e) {
            return "操作失败";
        }
        return "操作成功";
    }

    public String userUnfreeze(String userId, String reasonsForThawing) {
        UserInfo userInfo = userInfoService.findByUserId(Long.valueOf(userId));
        userInfo.setUserStatus("1");
        try {
            userInfoService.update(userInfo);
            stringRedisTemplate.delete(ConstantUtil.FREEZE_TIME+userId);
            stringRedisTemplate.delete(ConstantUtil.FREEZE_RANGE+userId);

            UserFreeze freeze = freezeService.findFreeze(Long.valueOf(userId));
            UserFreeze userFreeze = new UserFreeze();
            if (freeze!=null){
                userFreeze.setReasonsForThawing("解冻");
                freezeService.update(userFreeze,freeze.getId());

            }
            System.out.println(userFreeze);
            System.out.println(freeze);
        } catch (Exception e) {
            return "操作失败";
        }
        return "操作成功";
    }
}
